import React,{Component} from "react";
import "../Item/index.css"


export default class Item extends Component{
    // 鼠标的初始状态
    state = {mouse:false}

    // 鼠标移入移出效果
    handleMouse = (flag)=>{

        // 必须写成高阶函数
        return ()=>{
            // console.log(flag)
            this.setState({mouse:flag})
        }
    }
    // 勾选、取消处理勾选某一个的回调
    handleCheck = (id)=>{
        return (event)=>{
            // console.log(id,event.target.checked)
            this.props.updateTodo(id,event.target.checked)
        }
    }

    // 处理删除的回调
    handleDelete=(id)=>{
        console.log('删除',id)
        if(window.confirm('确认删除吗')){
            console.log('删除成功')
            this.props.deleteTodo(id)
        }
    }


    render() {
        const {name,done,id} = this.props
        // console.log('name',name)
        const {mouse} = this.state
        return(
            <li style={{background:mouse?'#ddd':'white'}} onMouseEnter={this.handleMouse(true)} onMouseLeave={this.handleMouse(false)}>
                <label>
                    <input type="checkbox" checked={done} onChange={this.handleCheck(id)}/>
                    <span>{name}</span>
                </label>
                <button onClick={()=>this.handleDelete(id)} className="btn btn-danger" style={{display:mouse?'block':'none'}}>删除</button>
            </li>
        )
    }
}
